package com.zyg.apart.controller;

import com.github.pagehelper.PageInfo;
import com.zyg.apart.model.Flats;
import com.zyg.apart.service.FlatsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;

/**
 * 房屋
 */
@Controller
@RequestMapping("/flats")
public class FlatsController {
	@Autowired
	private FlatsService flatsService;

	@RequestMapping("/list")
	public String index(Integer currentPage, Model model){
		if (currentPage == null){
			currentPage = 1;
		}
		PageInfo<Map<String,Object>> flats = flatsService.getAll(currentPage);
		model.addAttribute("flats",flats);
		return "flats/list";
	}

	@RequestMapping("/detail")
	public String detail(Model model,Integer id){
		Flats flat = flatsService.getFlatById(id);
		model.addAttribute("flat",flat);
		return "flats/detail";
	}

	@RequestMapping("/toEdit")
	public String toEdit(Model model, Integer id){
		Flats flat = flatsService.getFlatById(id);
		model.addAttribute("flat",flat);
		return "flats/edit";
	}

	@RequestMapping("/edit")
	public String edit(Flats flats){
		int i = flatsService.editFlats(flats);
		return "forward:list";
	}

	@RequestMapping("/del")
	public String del(Integer id){
		int i = flatsService.delFlatById(id);
		return "forward:list";
	}

	@RequestMapping("/toAdd")
	public String toAdd(){
		return "flats/add";
	}

	@RequestMapping("/add")
	public String add(Flats flats){
		System.out.println(flats);
		int i = flatsService.addFlats(flats);
		return "forward:list";
	}

}
